package com.example.mp.config;

import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.mp.entity.BaseEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @Author: LiYuhang
 * @Date: 2021/3/10 17:25
 * @Description: 字段自动填充
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, BaseEntity.GMT_CREATE, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, BaseEntity.GMT_MODIFIED, LocalDateTime::now, LocalDateTime.class);
        this.strictInsertFill(metaObject, BaseEntity.FIELD_CREATOR, this::getLoginUserName, String.class);
        this.strictInsertFill(metaObject, BaseEntity.FIELD_EDITOR, this::getLoginUserName, String.class);
        this.strictInsertFill(metaObject, BaseEntity.VERSION, Integer.class, 1);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, BaseEntity.GMT_MODIFIED, LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, BaseEntity.FIELD_EDITOR, this::getLoginUserName, String.class);
    }

    /**
     * 上下文use对象
     *
     * @return
     */
    private String getLoginUserName() {
        return "default";
    }

}
